UiHelper.TimeTip = obj:extend()
---text 计时器名字
---time 计时器对象
function UiHelper.TimeTip:new(text, time)

    self.texts = text
    self.time = time
    self.TextAlign = TEXT_ALIGN_CENTER

    self.bg = UiHelper.backdrop({fdfName="sysItemTip"}):dfsize(0.076,0.02):dfpointp(0,0, nil, FRAME_ALIGN_RIGHT_TOP, FRAME_ALIGN_RIGHT_TOP)
    self.text = UiHelper.label():dfpointp(0, 0, self.bg.frameId, FRAME_ALIGN_CENTER, FRAME_ALIGN_CENTER):dfsetTextAlignment(self.TextAlign)

    self:TimeText()
    self:setText(math.ceil(self.time.remain()))
    table.insert(AllTimeTip, self)
    self:Setpoint()
    return self
end
-- 根据秒数返回时间串
function  UiHelper.TimeTip:getTimeString(time)
    local txt={}
    local hours = math.floor(time / 3600)

    local minutes = math.floor((time % 3600) / 60)
    local seconds = math.floor(time % 60)
    if(hours < 10) then hours = "0"..hours end
    if(minutes < 10) then  minutes = "0"..minutes end
    if(seconds < 10) then seconds = "0"..seconds end 
    if hours~="00" then
        table.insert(txt, hours.."时")
    end
    if minutes~="00" then
        table.insert(txt, minutes.."分")
    end
    if seconds~="00" then
        table.insert(txt, seconds.."秒")
    end
    --local time = ""..hours.."时"..minutes.."分"..seconds.."秒"
    return table.concat(txt,"")
end

function UiHelper.TimeTip:setText(text)
    self.text:dftext(self.texts .. text )
    return self
end

function UiHelper.TimeTip:TimeText()
    self.TimeSetText = htime.setInterval(
        1,
        function(curTimer)
            --self:setText( math.ceil(self.time.elapsed()).."/"..self.time.period() )
           
            local timeNumber = math.ceil(self.time.remain())
            if timeNumber == 0 then
                self:Del()
            else
                local timef=self:getTimeString(timeNumber)
                self:setText(timef)
            end
        end
    )
    
end

function UiHelper.TimeTip:Setpoint()
    for index, value in ipairs(AllTimeTip) do
        if index - 1 >= 1 then
            value.bg:dfpoint(0, 0, FRAME_ALIGN_RIGHT_BOTTOM, AllTimeTip[index - 1].bg.frameId, FRAME_ALIGN_LEFT_BOTTOM):dfpoint(0, 0, FRAME_ALIGN_RIGHT_TOP, AllTimeTip[index - 1].bg.frameId, FRAME_ALIGN_LEFT_TOP)
        else
            value.bg:dfpointp(0, 0, nil, FRAME_ALIGN_RIGHT_TOP, FRAME_ALIGN_RIGHT_TOP)
        end
    end
end

function UiHelper.TimeTip:Del()
    hjapi.DzDestroyFrame(self.bg.frame.frameId)
    hjapi.DzDestroyFrame(self.text.frame.frameId)
    self.TimeSetText.destroy()
    -- table.remove(AllTimeTip,self)
    -- print_mbr(#AllTimeTip)
    for index, value in ipairs(AllTimeTip) do
        if value.time.__ID__ == self.time.__ID__ then
            table.remove(AllTimeTip, index)
        end
    end

    self:Setpoint()
end
